package com.library.settings.web.controller;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.DateTimeUtils;
import com.library.commons.utils.UUIDUtils;
import com.library.settings.pojo.Admin;
import com.library.settings.pojo.User;
import com.library.settings.service.AdminService;
import com.library.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/toRegister.do")
    public String toRegister(){
        return "settings/qx/user/register";
    }

    @RequestMapping("/settings/qx/user/register.do")
    @ResponseBody
    public Object register(User user){
        ReturnObject returnObject=new ReturnObject();
        user.setUserId(UUIDUtils.getUUID());
        user.setUserRegdate(DateTimeUtils.formatDateTime(new Date()));
        System.out.println(user);
        // 调用service层方法
        int res = userService.saveUser(user);
        if (res>0){
            returnObject.setCode(Constant.RETURN_SUCCESS);
        }else{
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("账号重复！");
        }
        return returnObject;
    }

    @RequestMapping("settings/qx/userLogin.do")
    @ResponseBody
    public Object userLogin(String userAccount, String userPassword, HttpSession session){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userAccount",userAccount);
        map.put("userPassword",userPassword);

        // 调用service方法查询用户
        User user = userService.queryUserByLoginActAndLogonPwd(map);
        ReturnObject returnObject=new ReturnObject();

        // 生成响应信息
        if(user==null){
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("用户名或密码错误！");
        }else{
            returnObject.setCode(Constant.RETURN_SUCCESS);
            // 把用户信息放入session域
            session.setAttribute(Constant.SESSION_USER,user);
        }
        return returnObject;
    }

    @RequestMapping("/settings/qx/managerLogin.do")
    @ResponseBody
    public Object managerLogin(String adminAccount, String adminPassword, HttpSession session){
        HashMap<String, Object> map = new HashMap<>();
        map.put("adminAccount",adminAccount);
        map.put("adminPassword",adminPassword);

        // 调用service方法查询用户
        Admin admin = adminService.queryAdminByLogActAndPassword(map);
        System.out.println(admin);
        ReturnObject returnObject=new ReturnObject();

        // 生成响应信息
        if(admin==null){
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("用户名或密码错误！");
        }else{
            returnObject.setCode(Constant.RETURN_SUCCESS);
            // 把用户信息放入session域
            session.setAttribute(Constant.SESSION_USER,admin);
        }
        return returnObject;
    }
}
